package java_DesignPattern.StructuralPattern.ProxyPattern.DynamicProxy.cjlib;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * cglib动态代理工厂
 */
public class ProxyFactory extends Enhancer {

    //声明目标类对象
    private TranStation station = new TranStation();

    public TranStation getProxyObject(){
        //设置目标类
        setSuperclass(TranStation.class);
        //设置回调方法
        setCallback(new MethodInterceptor() {
            @Override
            public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                //方法增强处
                System.out.println("代售点收取服务费用");
                //原始对象调用---相当于station.sell()
                return method.invoke(station,objects);
            }
        });
        //创建代理对象
        return (TranStation) create();
    }
}

